package de.fhd.pms.dao;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import de.fhd.pms.model.Tour;
import de.fhd.pms.model.User;

/** The data access class for Tours. */

public class TourDao  extends HibernateDaoSupport{
	

	/**
	 * <p>Set a {@link} User.</p>
	 */
	public void setUserDao(UserDao userDao) {
	}

	/**
	 * <p>Get a {@link} User.</p>
	 */
	public UserDao getUserDao() {
		return null;
	}
	
	private ActionDao actionDao = null;
	
	/**Get ActionDao
	 @return actionDao
	  */
	public ActionDao getActionDao() {
		return actionDao;
	}
	
	/** Set ActionDao
	 * @param actionDao  */
	public void setActionDao(ActionDao actionDao) {
		this.actionDao = actionDao;
	}

	/**
	 * <p> Searches the db for a Tour by specifying the tour_id. </p>
	 * @param tour_id
	 * @return {@link Tour} object
	 */
	public Tour findByTourId(int tour_id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Tour) template.get(Tour.class, tour_id);
	}
	
	/**
	 * <p>Find all Tours</p>
	 * @return A list of Tours.
	 */
	public List<Tour> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Tour.class);
	}
	
	/**
	 * <p>Saves a {@link Tour} specified by the parameter in the database. </p>
	 * @param Tour a {@link Tour} object that should be saved in the database. 
	 * @return the object {@link Tour} specified by the parameter.
	 */
	public Tour save(Tour tour) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(tour);
		return tour;
	}
	
	/**
	 * <p>Finds a {@link Tour} by specifying the user.</p>
	 * @param {@link User} 
	 * @return {@link Tour}  
	 */
	@SuppressWarnings("unchecked")
	public Set<Tour> findByUser(User user) {
		Session hibernateSession = getSession();
		// set criteria for tour class 
		Criteria crit = hibernateSession.createCriteria(Tour.class);
		crit.add(Restrictions.eq("user", user));
		// get tours as list
		List<Tour> tmp = crit.list();
		// initialise result as HashSet of Tour type objects 
		Set<Tour> result = new HashSet<Tour>();
		// add list elements in for cycle to the result,
		// which has Set type 
		for (Tour t : tmp) {
			result.add(t);
		}
		return result;
	}
	
	/**
	 * <p>Deletes a {@link Tour} by specifying Tour and User</p>
	 * @param {@link tour} - tour which needs to be deleted
	 * @param {@link user} - owner of tour
	 * @throws DaoException
	 */
	public void delete(Tour tour, User user) throws DaoException  {
		HibernateTemplate template = getHibernateTemplate();
		// get user id from tour, which needs to be deleted
		Integer tour_id = tour.getUser().getId();
		// get user id, which was given as tour owner parameter
		Integer user_id = user.getId();
		// check, that user is the same, which was given as the owner parameter
		if ( tour_id == user_id || true){
			template.delete(tour);
		} else {
			throw new DaoException("You are not allowed to delete this tour, because You did not create it!");
		}
	}
	
	/**
	 * <p>Deletes all Tours</p>
	 */
	public void deleteAll(){
		HibernateTemplate template = getHibernateTemplate();
		List <Tour> tours = template.loadAll(Tour.class);
		template.deleteAll( (Collection) tours );
	}
	
}
